package csm.batteryinfo;

import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.IBinder;
import android.view.View;
import android.widget.RemoteViews;

public class BatteryInfoService extends Service {
	BroadcastReceiver batteryReceiver = null;

	protected static int currentLevel = 0;
	static PendingIntent pendingIntent;

	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}

	@Override
	public void onCreate() {
		// Create an Intent to launch InfoDisplayActivity
		Intent intent = new Intent(this, InfoDisplayActivity.class);
		pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

		batteryReceiver = new BroadcastReceiver() {
			@Override
			public void onReceive(Context context, Intent intent) {
				int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
				if (Math.abs(currentLevel / 10 - level / 10) > 0) {
					// update the widget only if necessary
					updateWidgets(level);
					currentLevel = level;
				}
			}
		};
		IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
		registerReceiver(batteryReceiver, filter);
	}

	@Override
	public void onDestroy() {
		unregisterReceiver(batteryReceiver);
		// reset current level as we don't have any widget on screen.
		currentLevel = 0;
		super.onDestroy();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		return START_STICKY;
	}

	private void updateWidgets(int level) {
		AppWidgetManager appmanager = AppWidgetManager.getInstance(this);
		ComponentName cmpName = new ComponentName(this, BatteryInfoWidget.class);
		int[] widgetIds = appmanager.getAppWidgetIds(cmpName);

		updateWidgets(this, appmanager, widgetIds, level);
	}

	public static void updateWidgetsStatic(Context context,
			AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		// updateWidgets(context, appWidgetManager, appWidgetIds, currentLevel);
	}

	private void updateWidgets(Context context,
			AppWidgetManager appWidgetManager, int[] appWidgetIds, int level) {
		for (int wid : appWidgetIds) {
			RemoteViews rView = new RemoteViews(context.getPackageName(),
					R.layout.widget);

			// set invisible the lines above the level
			switch (level / 10) {
			case 0:
				rView.setViewVisibility(R.id.widgetLevelImageView2,
						View.INVISIBLE);

			case 1:
				rView.setViewVisibility(R.id.widgetLevelImageView3,
						View.INVISIBLE);

			case 2:
				rView.setViewVisibility(R.id.widgetLevelImageView4,
						View.INVISIBLE);

			case 3:
				rView.setViewVisibility(R.id.widgetLevelImageView5,
						View.INVISIBLE);

			case 4:
				rView.setViewVisibility(R.id.widgetLevelImageView6,
						View.INVISIBLE);

			case 5:
				rView.setViewVisibility(R.id.widgetLevelImageView7,
						View.INVISIBLE);

			case 6:
				rView.setViewVisibility(R.id.widgetLevelImageView8,
						View.INVISIBLE);

			case 7:
				rView.setViewVisibility(R.id.widgetLevelImageView9,
						View.INVISIBLE);

			case 8:
				rView.setViewVisibility(R.id.widgetLevelImageView10,
						View.INVISIBLE);
			}

			// set visible the lines under the level
			switch (level / 10) {
			case 9:
				rView.setViewVisibility(R.id.widgetLevelImageView10,
						View.VISIBLE);

			case 8:
				rView.setViewVisibility(R.id.widgetLevelImageView9,
						View.VISIBLE);

			case 7:
				rView.setViewVisibility(R.id.widgetLevelImageView8,
						View.VISIBLE);

			case 6:
				rView.setViewVisibility(R.id.widgetLevelImageView7,
						View.VISIBLE);

			case 5:
				rView.setViewVisibility(R.id.widgetLevelImageView6,
						View.VISIBLE);

			case 4:
				rView.setViewVisibility(R.id.widgetLevelImageView5,
						View.VISIBLE);

			case 3:
				rView.setViewVisibility(R.id.widgetLevelImageView4,
						View.VISIBLE);

			case 2:
				rView.setViewVisibility(R.id.widgetLevelImageView3,
						View.VISIBLE);

			case 1:
				rView.setViewVisibility(R.id.widgetLevelImageView2,
						View.VISIBLE);
			}

			// Get the layout for the App Widget and attach an on-click listener
			// to the button
			if (null != pendingIntent)
				rView.setOnClickPendingIntent(R.id.relativeLayout1,
						pendingIntent);

			appWidgetManager.updateAppWidget(wid, rView);
		}
	}

}
